﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Budget.MthWCFProxy;
using Newtonsoft.Json;
using Budget.HelperClasses;
using System.IO;

namespace Budget.Data
{
	public partial class Months : System.Web.UI.Page
	{
		protected void Page_Load( object sender, EventArgs e )
		{
			string action = Request.Params["xaction"];
			if ( !string.IsNullOrEmpty( action ) )
			{
				switch ( action )
				{
					case "getmonthbydate":
						GetMonthByDate();
						break;
					case "create":
						CreatMonth();
						break;
					case "update":
						UpdateMonth();
						break;
					case "destroy":
						DeleteMonth();
						break;
					default:
						ListMonths();
						break;
				}
			}
		}

		private void GetMonthByDate()
		{
			StreamReader inputDataStream = new StreamReader( Request.InputStream );
			string data = inputDataStream.ReadToEnd();
			DateTime today = JsonConvert.DeserializeObject<DateTime>( data );

			JsonResponse<Mth> response = new JsonResponse<Mth>();

			WCFHelper.Using<MthServiceClient>( svc => response.SingleData = svc.GetByMonthAndYear( today.Month, today.Year ) );

			response.Success = true;
			response.Message = "Successfully retrieved the current Month object.";

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );
		}

		#region Month Actions
		private void CreatMonth()
		{
			Mth newMth = JsonConvert.DeserializeObject<Mth>( Request.Form["data"] );
			newMth.Timestamp = DateTime.Now;

			JsonResponse<Mth> response = new JsonResponse<Mth>();

			try
			{
				WCFHelper.Using<MthServiceClient>( svc => svc.Save( newMth ) );

				response.Data = new Mth[] { newMth };
				response.Success = true;
				response.Message = "Successfully created Mth object.";
			}
			catch ( Exception ex )
			{
				response.Success = false;
				response.Message = "An error occurred while creating Mth object.";
			}

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );
		}

		private void UpdateMonth()
		{
			Mth updatedMthDetails = JsonConvert.DeserializeObject<Mth>( Request.Form["data"] );

			JsonResponse<Mth> response = new JsonResponse<Mth>();

			try
			{
				Mth mth = null;
				WCFHelper.Using<MthServiceClient>( svc => mth = svc.GetMthById( updatedMthDetails.MthId ) );

				mth.Month = updatedMthDetails.Month;
				mth.Year = updatedMthDetails.Year;
				mth.TotalIncome = updatedMthDetails.TotalIncome;
				mth.Timestamp = DateTime.Now;

				WCFHelper.Using<MthServiceClient>( svc => svc.Save( mth ) );

				response.Success = true;
				response.Message = "Successfully update Mth object.";
			}
			catch ( Exception ex )
			{
				response.Success = false;
				response.Message = "An error occurred while updating Mth object.";
			}

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );
		}

		private void DeleteMonth()
		{
			throw new NotImplementedException();
		}

		private void ListMonths()
		{
			JsonResponse<Mth> response = new JsonResponse<Mth>();

			WCFHelper.Using<MthServiceClient>( svc => response.Data = svc.GetAll() );
			
			response.Count = response.Data.Count();
			response.Success = true;
			response.Message = "Successfully retrieved all Mth objects.";

			ltrJsonResponse.Text = JsonConvert.SerializeObject( response );

		}
		#endregion
	}
}
